﻿using Newtonsoft.Json;
using System;
using CuteRibs.D3Armory.Resources;

namespace CuteRibs.D3Armory.Models
{
	public class BattleTag : IEquatable<BattleTag>, IComparable<BattleTag>
	{
		private string tag;

		public string Tag
		{
			get { return this.tag; }
			set
			{
				this.tag = value;
				this.SetNameNumber(this.tag);
			}
		}

		public string DisplayTag { get; set; }

		public string Name { get; private set; }

		public ushort Number { get; private set; }

		public BattleTag() { }

		public BattleTag(string tag)
		{
			this.Tag = tag.Replace('#', '-');
		}

		private void SetNameNumber(string tag)
		{
			if (!string.IsNullOrWhiteSpace(tag))
			{
				try
				{
					int index = tag.IndexOf('-');

					if (index > 0)
					{
						this.DisplayTag = tag.Replace('-', '#');
						this.Name = tag.Substring(0, index);
						this.Number = ushort.Parse(tag.Substring(index + 1));
					}
				}
				catch (Exception ex)
				{
					throw new ArgumentException(AppRes.MsgInvalidBattleTag, ex);
				}
			}
		}

		public override int GetHashCode()
		{
			return this.Tag.GetHashCode();
		}

		public override string ToString()
		{
			return this.DisplayTag;
		}

		public bool Equals(BattleTag other)
		{
			return this.Tag.Equals(other.Tag, StringComparison.InvariantCultureIgnoreCase);
		}

		public int CompareTo(BattleTag other)
		{
			return this.Tag.CompareTo(other.Tag);
		}
	}
}
